package cn.sciento.config.client.api.impl;

import cn.sciento.config.client.api.ConfigClient;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class NacosConfigClient implements ConfigClient {
    private static final Logger LOGGER = LoggerFactory.getLogger(NacosConfigClient.class);

    @Autowired
    private NacosConfigProperties nacosConfigProperties;

    @Autowired
    private NacosConfigManager nacosConfigManager;

    public void publishConfig(String env, String serviceName, String version, String fileType, String content) {
        ConfigService configService = nacosConfigManager.getConfigService();

        try {
            configService.publishConfig(serviceName + "-" + env + "." + fileType, this.nacosConfigProperties.getGroup(), content);
        } catch (NacosException e) {
            LOGGER.error("publish config failed", e);
        }
    }
}
